package com.definity.toolkit.ui.widget;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.Icon;

import com.definity.toolkit.ui.Align;
import com.definity.toolkit.ui.action.ActionExecutor;

public class ActionIcon extends BorderPanel {

	private static final long serialVersionUID = -696281673420682171L;

	private final WidgetBuilder b = new WidgetBuilder();
	
	public ActionIcon(boolean autoHide, final String label, String tooltip, Icon icon, ActionExecutor executor) {
		opaque(false);
		
		final Label text = b.label(label).size(0, 10).action(executor);
		text.font(text.font().deriveFont(11f)).foreground(Color.WHITE).horizontalAlignment(Align.CENTER);
		top(text);
			
		Label action = b.label(icon).tooltip(tooltip).action(executor);
		action.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		action.border(BorderFactory.createEmptyBorder(5, 0, 0, 0));
		if (autoHide) {
			text.text("");
			action.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseEntered(MouseEvent e) {
					text.text(label);
				}
				@Override
				public void mouseExited(MouseEvent e) {
					text.text("");
				}
			});
		}
		center(action);
	}
	
}
